
package pe.pucp.netbeans.pluggin.mda;

import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import org.openide.util.Exceptions;
import pe.pucp.netbeans.common.util.LineaComando;

final class SpringRooPanel extends javax.swing.JPanel {

    private final SpringRooOptionsPanelController controller;

    private JFileChooser fileChooser;

    SpringRooPanel(SpringRooOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                String s = f.getName();
                return (f.isDirectory() || s.equals("roo.sh") || s.equals("roo.bat"));
            }

            @Override
            public String getDescription() {
                return ".sh, .bat";
            }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblRutaSpringRoo = new javax.swing.JLabel();
        txtRutaSpringRoo = new javax.swing.JTextField();
        btnRutaSpringRoo = new javax.swing.JButton();
        btnProbar = new javax.swing.JButton();
        pnlTest = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaTest = new javax.swing.JTextArea();

        org.openide.awt.Mnemonics.setLocalizedText(lblRutaSpringRoo, org.openide.util.NbBundle.getMessage(SpringRooPanel.class, "SpringRooPanel.lblRutaSpringRoo.text")); // NOI18N

        txtRutaSpringRoo.setText(org.openide.util.NbBundle.getMessage(SpringRooPanel.class, "SpringRooPanel.txtRutaSpringRoo.text")); // NOI18N
        txtRutaSpringRoo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRutaSpringRooActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnRutaSpringRoo, org.openide.util.NbBundle.getMessage(SpringRooPanel.class, "SpringRooPanel.btnRutaSpringRoo.text")); // NOI18N
        btnRutaSpringRoo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRutaSpringRooActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnProbar, org.openide.util.NbBundle.getMessage(SpringRooPanel.class, "SpringRooPanel.btnProbar.text")); // NOI18N
        btnProbar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProbarActionPerformed(evt);
            }
        });

        pnlTest.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        txaTest.setColumns(20);
        txaTest.setEditable(false);
        txaTest.setRows(5);
        jScrollPane1.setViewportView(txaTest);

        javax.swing.GroupLayout pnlTestLayout = new javax.swing.GroupLayout(pnlTest);
        pnlTest.setLayout(pnlTestLayout);
        pnlTestLayout.setHorizontalGroup(
            pnlTestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTestLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlTestLayout.setVerticalGroup(
            pnlTestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTestLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblRutaSpringRoo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtRutaSpringRoo, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnRutaSpringRoo))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnProbar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRutaSpringRoo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRutaSpringRoo)
                    .addComponent(lblRutaSpringRoo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnProbar, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(pnlTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        btnProbar.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(SpringRooPanel.class, "SpringRooPanel.btnProbar.AccessibleContext.accessibleName")); // NOI18N
    }// </editor-fold>//GEN-END:initComponents

    private void txtRutaSpringRooActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRutaSpringRooActionPerformed
        
    }//GEN-LAST:event_txtRutaSpringRooActionPerformed

    private void btnRutaSpringRooActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRutaSpringRooActionPerformed
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            txtRutaSpringRoo.setText( file.getAbsolutePath() );
        } else {
            System.out.println("File access cancelled by user.");
        }
    }//GEN-LAST:event_btnRutaSpringRooActionPerformed

    private void btnProbarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProbarActionPerformed
        if (txtRutaSpringRoo.getText() == null || txtRutaSpringRoo.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "mensaje", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            String text = txtRutaSpringRoo.getText();
            LineaComando lc;
            try {
                lc = new LineaComando(text, txaTest, false);
                lc.nuevoComando("version");
                lc.nuevoComando("quit");
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Hubo un error al ejecutar programa "+text+".\nAsegúrese que haya elegido el programa correcto de spring roo.", "Error", JOptionPane.ERROR_MESSAGE);
            }
            
        }
    }//GEN-LAST:event_btnProbarActionPerformed

    void load() {
        // TODO read settings and initialize GUI
        // Example:        
        // someCheckBox.setSelected(Preferences.userNodeForPackage(SpringRooPanel.class).getBoolean("someFlag", false));
        // or for org.openide.util with API spec. version >= 7.4:
        // someCheckBox.setSelected(NbPreferences.forModule(SpringRooPanel.class).getBoolean("someFlag", false));
        // or:
        // someTextField.setText(SomeSystemOption.getDefault().getSomeStringProperty());
        txtRutaSpringRoo.setText(Preferences.userNodeForPackage(SpringRooPanel.class).get("rutaSpringRoo",null));
        txaTest.setText(null);
    }

    void store() {
        // TODO store modified settings
        // Example:
        Preferences.userNodeForPackage(SpringRooPanel.class).put("rutaSpringRoo", txtRutaSpringRoo.getText());
        // Preferences.userNodeForPackage(SpringRooPanel.class).putBoolean("someFlag", someCheckBox.isSelected());
        // or for org.openide.util with API spec. version >= 7.4:
        // NbPreferences.forModule(SpringRooPanel.class).putBoolean("someFlag", someCheckBox.isSelected());
        // or:
        // SomeSystemOption.getDefault().setSomeStringProperty(someTextField.getText());
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnProbar;
    private javax.swing.JButton btnRutaSpringRoo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblRutaSpringRoo;
    private javax.swing.JPanel pnlTest;
    private javax.swing.JTextArea txaTest;
    private javax.swing.JTextField txtRutaSpringRoo;
    // End of variables declaration//GEN-END:variables
   
}
